<?php

/**
 * MyProjectNameHere <http://www.example.com>
 * Model Class
 *
 * It is recommended to extend Model classes from WWW_Factory in order to 
 * provide various useful functions and API access for the Model.
 *
 * @package    Factory
 * @author     DeveloperNameHere <email@example.com>
 * @copyright  Copyright (c) 2012, ProjectOwnerNameHere
 * @license    Unrestricted
 * @tutorial   /doc/pages/guide_mvc.htm
 * @since      1.0.0
 * @version    1.0.0
 */

class WWW_model_topstories extends WWW_Factory {

	/**
	 * It is recommended to define all data variables here. Usually the 
	 * data variables have the same names as the column names of database 
	 * rows from a table.
	 */	
	public $conn;
	/**
	 * Alternative to __construct()
	 *
	 * WWW_Factory does not allow to overwrite __construct() method, so 
	 * this __initialize() is used instead and loaded automatically when 
	 * object is created.
	 *
	 * @return boolean
	 */
	public function __initialize(){
		require('engine/class.www-database.php');
		$this->conn=new WWW_Database();				
		
		require('engine/include_dao.php');
		return true;
		
	}
	public function create_topstories($data)
	{		
			$file = $data['topstories_image']['tmp_name'];
			$filename = time().$data["topstories_image"]['name'];
			$topstories_image_path="uploads/events/".$filename;
			move_uploaded_file($file, $topstories_image_path);
		
		$topStorie = new TopStorie();
		$topStorie->topstories_title = $data['topstories_title'];
		$topStorie->topstories_date = $data['topstories_date'];
		$topStorie->topstories_content = $data['topstories_content'];
		$topStorie->topstories_author = $data['topstories_author'];
		$topStorie->topstories_image = $this->conn->baseurl().$topstories_image_path;
		$topStorie->created_date = date("Y-m-d H:i:s");
		$topStorie->updated_date = date("Y-m-d H:i:s");
		$result = DAOFactory::getTopStoriesDAO()->insert($topStorie);
		//print_r($result);exit;
		
	/* $query="insert into `top_stories` (topstories_title,topstories_date,topstories_content,topstories_author,topstories_image,created_date,updated_date) VALUES ('".$data['topstories_title']."','".$data['topstories_date']."','".$data['topstories_content']."','".$data['topstories_author']."','".$this->conn->baseurl().$topstories_image_path."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
	//print_r($query);exit;
	$result=$this->conn->dbCommand($query);	 */
		if($result !== ''){
			return array('message'=>'Top stories successfully created');			
		}else{
			return array('message'=>'Top stories Creation Failed');
		}	
	}
	public function edit_topstories($data)
	{
		if($data["topstories_image"]!=""){
			$file = $data['topstories_image']['tmp_name'];
			$filename = time().$data["topstories_image"]['name'];
			$topstories_image_path="uploads/events/".$filename;
			move_uploaded_file($file, $topstories_image_path);
			}
			else 
			{
				$topstories_image_path = "";
			}
			
			$topStorie = new TopStorie();
		$topStorie->topstories_id = $data['topstories_id'];
		$topStorie->topstories_title = $data['topstories_title'];
		$topStorie->topstories_date = $data['topstories_date'];
		$topStorie->topstories_content = $data['topstories_content'];
		$topStorie->topstories_author = $data['topstories_author'];
		$topStorie->topstories_image = $this->conn->baseurl().$topstories_image_path;
		$topStorie->updated_date = date("Y-m-d H:i:s");
		$topStorie->status = $data['status'];
		$result = DAOFactory::getTopStoriesDAO()->update($topStorie);
		//print_r($result);exit;
			
	/* $query="update `top_stories` set topstories_title='".$data['topstories_title']."',topstories_date='".$data['topstories_date']."',topstories_content='".$data['topstories_content']."',topstories_author='".$data['topstories_author']."'".($$topstories_image_path!="" ? ',topstories_image='.$this->conn->baseurl().$$topstories_image_path : '').",updated_date='".date("Y-m-d H:i:s")."' where topstories_id=".$data['topstories_id'];
		//print_r($query);exit;
		$result=$this->conn->dbCommand($query);	 */	
		if($result !== 0){
			return array('message'=>'Top stories Updated Successfully');			
		}else{
			return array('message'=>'Top stories Updates Failed');
		}	
	
	}
	public function get_topstories($data)
	{	
			$topStorie = new TopStorie();
		$topStorie->topstories_id = $data['topstories_id'];
		$result = DAOFactory::getTopStoriesDAO()->load($topStorie);
		//print_r($result);exit;
		
		/*  $query="SELECT topstories_title,topstories_date,topstories_content,topstories_author,topstories_image from top_stories
where topstories_id=".$data['topstories_id'];
		$result=$this->conn->dbMultiple($query); */
		
		if($result !== ''){
			return array('message'=>'Top stories get Successfully', 'result'=>$result);			
		}else{
			return array('message'=>'Top stories get Failed');
		}	
		
	}
	public function delete_topstories($data){
			$topStorie = new TopStorie();
		$topStorie->topstories_id = $data['topstories_id'];
		$topStorie->status = $data['status'];
		$result = DAOFactory::getTopStoriesDAO()->delete_topStories($topStorie);
		//print_r($result);exit;
		
		$data=$this->conn->dbCommand("update `top_stories` set status='".$data['status']."' where topstories_id=".$data['topstories_id']);
		if($result == 1)
			return array('message'=>"Top stories Deleted Successfully");		
		else
			return array('message'=>'Top stories Deletion Fails');
	}
	public function all_topstories($data){
		$current_page=0;
	$results_per_page=10;
	$order_by="created_date";
	$sort_order="desc";
	if(isset($data['order_by']) && $data['order_by']!='')
	{
		$order_by=$data['order_by'];
	}
	if(isset($data['results_per_page']) && $data['sort_order']!='')
	{
		$sort_order=$data['sort_order'];
	}
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
	/* $query_string="select * from `top_stories`";
	$query_string1="SELECT count(*) as total from top_stories"; */
	if (isset($data['search_by']) && $data['search_by']!='')
	{	//print_r($data);exit;
		if($data['search_by']=='topstories_title')
		{
			$search_by = "topstories_title";
			
		}else if($data['search_by'] == 'topstories_author')
		{
			$search_by = "topstories_author";
			
		}
		
			$topStorie = new TopStorie();
		$topStorie->order_by = $order_by;
		$topStorie->sort_order = $sort_order;
		$topStorie->results_per_page = $results_per_page;
		$topStorie->offset = $offset;
		$topStorie->search_by = $search_by;
		$topStorie->search_by_value = $data['search_by_value'];
		$result = DAOFactory::getTopStoriesDAO()->queryAllSearchBy($topStorie);
		$totalrows = DAOFactory::getTopStoriesDAO()->searchBy_count($topStorie);
		//print_r($result);exit;
		
		/* $query =$query_string." where ".$search_by." like '%".$data['search_by_value']."%' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		$query1 =$query_string1." where ".$search_by." like '%".$data['search_by_value']."%'"; */
		
	}
	else
	{	
	$topStorie = new TopStorie();
		$topStorie->order_by = $order_by;
		$topStorie->sort_order = $sort_order;
		$topStorie->results_per_page = $results_per_page;
		$topStorie->offset = $offset;
		$result = DAOFactory::getTopStoriesDAO()->queryAll($topStorie);
		$totalrows = DAOFactory::getTopStoriesDAO()->topStories_count($topStorie);
		//print_r($result);exit;
		/* $query = "select * from `top_stories` order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
	$query1 = "SELECT count(*) as total from top_stories"; 
	}	//print_r($query);exit;
		$result=$this->conn->dbMultiple($query);*/
	}
	if($result !== ''){
			//$totalrows = $this->conn->dbSingle($query1);
		return array('message'=>"All Top stories","info"=>$result,'total'=>$totalrows,'current_page'=>$current_page,'results_per_page'=>$results_per_page);
		}
		else
		return array('message'=>'No Top stories');
}
}
	
?>